﻿Public Class frmInstr
    Private LastTxtBox As TextBox

    Private Sub frmInstr_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadPrepare()

    End Sub
    Private Sub LoadPrepare()
        Dim mPrjOpt As New LevelDatabase.CurrentOption
        'Dim mDB As New DbLevel
        mPrjOpt = MyShared.myDb.CurrentRead()
        If mPrjOpt.CurrentProject = String.Empty Then
            MsgBox("请设置当前工程！")
            Me.Close()
        Else
            lblPrj.Text = mPrjOpt.CurrentProject
            txtInstr.Text = mPrjOpt.InstrNo
            txtStaff1.Text = mPrjOpt.Staff1
            txtStaff2.Text = mPrjOpt.Staff2
        End If
    End Sub



    Private Sub txtInstr_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
    txtInstr.GotFocus, txtStaff1.GotFocus, txtStaff2.GotFocus
        LastTxtBox = CType(sender, TextBox)
        UpdateList()
    End Sub
    Private Sub UpdateList()
        'Dim mDb1 As New DbLevel
        Dim sTmp As String = IIf(LastTxtBox.Name = "txtInstr", MyShared.myDb.InstrRead("水准仪"), MyShared.myDb.InstrRead("水准尺"))
        Dim sv() As String = sTmp.Split(vbCrLf)
        ListBox1.Items.Clear()
        For Each su As String In sv
            If su.Trim.Length > 0 Then
                ListBox1.Items.Add(su.Trim)
            End If
        Next
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles ListBox1.SelectedIndexChanged
        Dim sv() As String = ListBox1.SelectedItem.ToString.Split(",")
        If sv.Length = 2 Then
            LastTxtBox.Text = sv(1)
        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim mPrjOpt As New LevelDatabase.CurrentOption
        mPrjOpt.InstrNo = txtInstr.Text
        mPrjOpt.Staff1 = txtStaff1.Text
        mPrjOpt.Staff2 = txtStaff2.Text

        'Dim mDB As New DbLevel
        MyShared.myDb.CurrentRecord(mPrjOpt)
        Me.Close()
    End Sub


    Private Sub txtInstr_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
    Handles txtInstr.Validating, txtStaff1.Validating, txtStaff2.Validating
        Dim txt As TextBox
        txt = CType(sender, TextBox)

        Dim sTmp As String = CType(sender, TextBox).Text.Trim
        If sTmp.Length > 0 Then
            'Dim mDb As New DbLevel
            Dim InstrType As String = IIf(txt.Name = "txtInstr", "水准仪", "水准尺")
            MyShared.myDb.InstrRecordRow(InstrType, sTmp)
            UpdateList()
        End If
    End Sub


    Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Dim mDb As New DbLevel
        Dim sTmp As String = ListBox1.SelectedItem.ToString.Trim
        Dim iPos As Integer = InStr(sTmp, ",")
        Dim sType As String = sTmp.Substring(0, iPos - 1)
        Dim sNum As String = sTmp.Substring(iPos)

        If sType.Length > 0 And sNum.Length > 0 Then
            MyShared.myDb.InstrDeleteRow(sType, sNum)
            LastTxtBox.Text = ""
            UpdateList()
        End If
    End Sub

End Class